package com.baiyang.app.ui.read.commentList.addComment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.baiyang.app.HomeActivity;
import com.baiyang.app.MyApp;
import com.baiyang.app.R;
import com.baiyang.app.databinding.ActivityAddCommentBinding;
import com.baiyang.app.model.User;

/**
 * todo:给toolbar右侧文本设置颜色
 */
public class AddCommentActivity extends AppCompatActivity {

    private ActivityAddCommentBinding binding;
    private AddCommentViewModel viewModel;
    private AddCommentService service;
    private float rat;
    private String comment;
    private String articleId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_add_comment);
        viewModel = new ViewModelProvider(this).get(AddCommentViewModel.class);
        service = new AddCommentService(getApplicationContext(),viewModel);
        binding.toolbar.setTitle("");
        setSupportActionBar(binding.toolbar);
        articleId = getIntent().getStringExtra("articleId");
        binding.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                rat = rating;
            }
        });
        initHandler();
    }

    private void initHandler() {
        viewModel.getAddResult().observe(this,e->{
            String result = e.getString("result");
            String detail = e.getString("detail");
            if("SUCCESS".equals(result)){
                Toast.makeText(getApplicationContext(),"评论成功",Toast.LENGTH_SHORT).show();
                finish();
                overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
            }else{
                Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.add_comment_menu, menu);
        View view = menu.findItem(R.id.add).getActionView();
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == R.id.add){
            if(rat == 0f){
                Toast.makeText(getApplicationContext(),"评分总不可能是0吧~~",Toast.LENGTH_SHORT).show();
                return true;
            }
            comment = binding.comment.getText().toString();
            if("".equals(comment)){
                Toast.makeText(getApplicationContext(),"请输入评论内容",Toast.LENGTH_SHORT).show();
                return true;
            }
            service.addComment(rat,comment,articleId);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
}